﻿using Serilog.Core;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Log.SerilogExtend
{
    public class SerilogConverter
    {
        /// <summary>
        /// MinimumLevel字符串转LogEventLevel
        /// </summary>
        /// <param name="minimumLevelString"></param>
        /// <param name="defaultLogEventLevel"></param>
        public static LogEventLevel LogLevelStringToLogEventLevel(string minimumLevelString, LogEventLevel defaultLogEventLevel = LogEventLevel.Information)
        {
            if (minimumLevelString.ToLower() == LogEventLevel.Verbose.ToString().ToLower())
            {
                return LogEventLevel.Verbose;
            }
            else if(minimumLevelString.ToLower() == LogEventLevel.Debug.ToString().ToLower())
            {
                return LogEventLevel.Debug;
            }
            else if(minimumLevelString.ToLower() == LogEventLevel.Information.ToString().ToLower())
            {
                return LogEventLevel.Information;
            }
            else if(minimumLevelString.ToLower() == LogEventLevel.Warning.ToString().ToLower())
            {
                return LogEventLevel.Warning;
            }
            else if (minimumLevelString.ToLower() == LogEventLevel.Error.ToString().ToLower())
            {
                return LogEventLevel.Error;
            }
            else if (minimumLevelString.ToLower() == LogEventLevel.Fatal.ToString().ToLower())
            {
                return LogEventLevel.Fatal;
            }
            else
            {
                return defaultLogEventLevel;
            }
        }

        /// <summary>
        /// MicrosoftMinimumLevel字符串转LogEventLevel
        /// </summary>
        /// <param name="minimumLevelString"></param>
        /// <param name="defaultLogEventLevel"></param>
        public static LogEventLevel MicrosoftLogLevelStringToLogEventLevel(string minimumLevelString, LogEventLevel defaultLogEventLevel = LogEventLevel.Information)
        {
            /******************
            * 0. MS.Trace => Verbose : 详细 
            * 1. MS.Debug => Debug : 调试 
            * 2. MS.Information => Information : 信息 
            * 3. MS.Warning => Warning : 警告 
            * 4. MS.Error => Error : 错误 
            * 5. MS.Critical => Fatal : 致命 
            * 6. MS.None =>   : 不输出
            ******************/
            if (minimumLevelString.ToLower() == "Trace".ToLower() || minimumLevelString == "0")
            {
                return LogEventLevel.Verbose;
            }
            else if (minimumLevelString.ToLower() == "Debug".ToLower() || minimumLevelString == "1")
            {
                return LogEventLevel.Debug;
            }
            else if (minimumLevelString.ToLower() == "Information".ToLower() || minimumLevelString == "2")
            {
                return LogEventLevel.Information;
            }
            else if (minimumLevelString.ToLower() == "Warning".ToLower() || minimumLevelString == "3")
            {
                return LogEventLevel.Warning;
            }
            else if (minimumLevelString.ToLower() == "Error".ToLower() || minimumLevelString == "4")
            {
                return LogEventLevel.Error;
            }
            else if (minimumLevelString.ToLower() == "Critical".ToLower() || minimumLevelString == "5")
            {
                return LogEventLevel.Fatal;
            }
            else if (minimumLevelString.ToLower() == "None".ToLower() || minimumLevelString == "6")
            {
                //微软不输出任何日志，故此处返回最高级别
                return LogEventLevel.Fatal;
            }
            else
            {
                return defaultLogEventLevel;
            }
        }
    }
}
